package sdfi.lee.DAO;

import java.sql.Connection;
import java.sql.Statement;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import sdfi.lee.Entity.Person;

public class PersonDAO {
	public String personAdd(Person person)
	{
		Session session=null;
		Transaction ts=null;
		try
		{
			session=DAO.openSession();
			ts=session.beginTransaction();
			session.save(person);
			ts.commit();
			return "success";
		}
		catch(Exception ee)
		{
			System.out.println("Exception ee when add person");
			ee.printStackTrace();
			ts.commit();
			return "error";
		}
		finally
		{
			DAO.closeSession();
		}
		
	}
	public String personModify(Person person)
	{
		Session session=null;
		Transaction ts=null;
		try
		{
			session=DAO.openSession();
			ts=session.beginTransaction();
			Person pn=(Person)session.load(Person.class, person.getId());
			pn.setPersondescribe(person.getPersondescribe());
			pn.setPersonname(person.getPersonname());
			pn.setPersontype(person.getPersontype());
			session.update(pn);
			ts.commit();
		}
		catch(Exception ee)
		{
			System.out.println("Exception ee when modify person");
			ts.rollback();
			ee.printStackTrace();
		}
		finally
		{
			DAO.closeSession();
		}
		return "success";
	}
	public List<Person> getAllPerson(int page,int size,int type)
	{
		List<Person> list=null;
		Session session=null;
		Transaction ts=null;
		try
		{
			session=DAO.openSession();
			ts=session.beginTransaction();
			Query query=null;
			if(type==0)
			query=session.createQuery("from Person");
			else
			{
			query=session.createQuery("from Person as a where a.persontype=:type");
			query.setInteger("type", type);
			}
			query.setFirstResult((page-1)*size);
			query.setMaxResults(size);
			list=query.list();
	
			ts.commit();
		}
		catch(Exception ee)
		{
			System.out.println("Exception when get All Person");
			ee.printStackTrace();
			
		}
		return list;
	}
	public Person getPersonById(Integer id)
	{
		Person person=null;
		Session session=null;
		Transaction ts=null;
		try
		{
			session=DAO.openSession();
			ts=session.beginTransaction();
			person=(Person)session.get(Person.class, id);
			ts.commit();
		}
		catch(Exception ee)
		{
			System.out.println("Exception when get Person by id");
			ee.printStackTrace();
			ts.commit();
		}
		finally
		{
			DAO.closeSession();
		}
		return person;
	}
	public String personDelete(Integer id) throws Exception
	{
		Session session=DAO.openSession();
		Connection con=session.connection();
		try
		{
			Statement sts=con.createStatement();
			con.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
			sts.execute("delete from person where id='"+id.toString()+"'");
			con.commit();
			
		}
		catch(Exception ee)
		{
			con.rollback();
			ee.printStackTrace();
			return "error";
		}
		return "success";
	}
}
